package com.yanyu.space.codegen.controller;


import com.yanyu.space.codegen.bean.po.Attr;
import com.yanyu.space.codegen.bean.vo.attr.AttrSearcheVo;
import com.yanyu.space.codegen.service.IAttrService;
import com.yanyu.space.common.core.bean.db.PageEntity;
import com.yanyu.space.common.core.bean.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 模板属性 前端控制器
 *
 * @author yanyu
 * @date 2020/12/1
 */
@RestController
@RequestMapping("/codegen/attr")
@Api(tags = "模板属性")
@Validated
@AllArgsConstructor
public class AttrController {

    private IAttrService service;

    @GetMapping("/list")
    @ApiOperation(value = "查询模板属性信息")
    public R list(@Validated AttrSearcheVo vo) {
        PageEntity<Attr> list = service.list(vo);
        return R.ok(list);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询模板属性信息")
    public R get(@PathVariable("id") Long id) {
        Attr attr = service.getById(id);
        return R.ok(attr);
    }

    @GetMapping("/list/type")
    @ApiOperation(value = "查询模板属性信息,按照类型分组")
    public R listTypeToGroup() {
        Map<String, List<Attr>> result = service.listType();
        return R.ok(result);
    }

    @PostMapping
    @ApiOperation(value = "新增模板属性")
    public R add(@RequestBody Attr vo) {
        service.save(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改模板属性")
    public R update(@RequestBody Attr vo) {
        service.updateById(vo);
        return R.ok();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除模板属性")
    public R delete(@PathVariable("ids") List<Long> ids) {
        service.removeByIds(ids);
        return R.ok();
    }
}

